package com.hmjk.health.home;

import com.hmjk.health.HealthApp;
import com.hmjk.health.FocusFragment;
import com.hmjk.health.R;
import com.hmjk.health.background.DataManager;
import com.hmjk.health.setting.SettingsConstants;
import com.hmjk.health.views.PagerIndexStrip;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import android.widget.ImageView.ScaleType;
import android.view.MotionEvent;

public class LocateHomeFragment extends FocusFragment implements OnClickListener/*, OnTouchListener*/{
	private final String TAG = LocateHomeFragment.class.getSimpleName();
	private HealthApp mApp = null;
	private ViewSwitcher mADSwitcher = null;
	private PagerIndexStrip mADIndexView = null;
	private int mADIndex = 0;
	private float ad_downx;
	private Animation left2right_in;
	private Animation right2left_out;
	private Animation left2right_out;
	private Animation right2left_in;
	private ImageView scanView;
	private TextView to_map;
	private TextView to_cards;
	private int mADCount = 0;
	//private View mWhiteArea;
	//private LinearLayout mCardTextView;
	
	private void logd(String info){
		Log.d(TAG,"this =" + LocateHomeFragment.this + info);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		logd("onCreateView");
		mApp = (HealthApp)getActivity().getApplication();
		
		View v = inflater.inflate(R.layout.home, container, false);
		
		mADCount = mApp.getADnumber();
		mADIndex = 0;
		
		scanView = (ImageView) v.findViewById(R.id.scan);
		scanView.setOnClickListener(this);
		
		ImageView index = (ImageView) v.findViewById(R.id.toindex);
		index.setOnClickListener(this);
		
		to_map = (TextView)v.findViewById(R.id.to_map);
		String maphtmlstr = getResources().getString(R.string.enter_map);
		to_map.setText(Html.fromHtml(maphtmlstr));
		to_cards = (TextView)v.findViewById(R.id.to_cards);
		String cardhtmlstr = getResources().getString(R.string.enter_locacard);
		to_cards.setText(Html.fromHtml(cardhtmlstr));
		
		View cv = v.findViewById(R.id.card_act_view);
		View mv = v.findViewById(R.id.map_act_view);
		cv.setOnClickListener(this);
		mv.setOnClickListener(this);
		//mWhiteArea = v.findViewById(R.id.white_area);
		//mCardTextView = (LinearLayout) v.findViewById(R.id.card_layout);
		
		//to_map.setOnTouchListener(this);
		to_map.setOnClickListener(this);
		//to_cards.setOnTouchListener(this);
		to_cards.setOnClickListener(this);
		
		left2right_in = AnimationUtils.loadAnimation(getActivity(), R.anim.l_to_r_in_s);
		right2left_out = AnimationUtils.loadAnimation(getActivity(), R.anim.r_to_l_out_s);
		right2left_in = AnimationUtils.loadAnimation(getActivity(), R.anim.r_to_l_in_s);
		left2right_out = AnimationUtils.loadAnimation(getActivity(), R.anim.l_to_r_out_s);
		mADSwitcher = (ViewSwitcher)v.findViewById(R.id.ad_switcher);
		mADSwitcher.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent e) {
				if(e.getAction() == MotionEvent.ACTION_DOWN){
					ad_downx = e.getX();
					mApp.ADDebug("Fragment, touch ACTION_DOWN, x=" + ad_downx);
					return true;
				}else if(e.getAction() == MotionEvent.ACTION_UP){
					float up = e.getX();
					mApp.ADDebug("Fragment, touch ACTION_UP, x=" + up);
					int count = mApp.getADnumber();
					if(count == 0){
						return true;
					}
					if(count == 1){
						//only one
						return true;
					}
					if(ad_downx - up > 100){
						/*mADIndex ++;
						mADIndex = mADIndex%count;
						mADSwitcher.setInAnimation(right2left_in);
						mADSwitcher.setOutAnimation(right2left_out);
						updateADView();*/
					}else if(up - ad_downx > 100){
						/*if(mADIndex > 0)
							mADIndex --;
						else
							mADIndex = count-1;
						mADSwitcher.setInAnimation(left2right_in);
						mADSwitcher.setOutAnimation(left2right_out);
						updateADView();*/
					}else{
						mApp.openADLink(indexToKey(mADIndex));
					}
					return true;
				}
				return false;
			}
		});
		mADIndexView = (PagerIndexStrip)v.findViewById(R.id.ad_index);
		mADIndexView.setCount(Math.max(mADCount, 1));
		if(mADCount > 1){
			mADIndexView.setVisibility(View.VISIBLE);
		}else{
			mADIndexView.setVisibility(View.INVISIBLE);
		}
		
		mADIndex = 0;
		mADSwitcher.setDisplayedChild(0);
		refreshDevList();
		return v;
	}
	
	//private void resizeCardView(){
	//	int wh = mWhiteArea.getMeasuredHeight();
	//	int ch = mCardTextView.getMeasuredHeight();
	//	int cw = mCardTextView.getMeasuredWidth();
	
	//}
	
	private void refreshDevList(){
		if(mApp.isUserSignIn()){
		mApp.getDataManager().refreshData(DataManager.F_DEVICELIST);
		}
	}
	
	private String indexToKey(int index){
		String key = SettingsConstants.KEY_AD_01;
		if(index == 1){
			key = SettingsConstants.KEY_AD_02;
		}else if(index == 2){
			key = SettingsConstants.KEY_AD_03;
		}
		return key;
	}
	
	private void clearADView(){
		View v = mADSwitcher.getCurrentView();
		ImageView adimg = (ImageView)v.findViewById(R.id.ad_image);
		adimg.setImageResource(R.drawable.ad_default);
		adimg.setScaleType(ScaleType.FIT_XY);
	}
	
	private void updateADView(){
		mApp.ADDebug("updateADView, mADIndex=" + mADIndex);
		View v = mADSwitcher.getNextView();
		ImageView adimg = (ImageView)v.findViewById(R.id.ad_image);
		TextView adtxt = (TextView)v.findViewById(R.id.ad_text);
		mApp.showAD(adimg, adtxt, indexToKey(mADIndex));
		mADIndexView.setCurrent(mADIndex);
		mADIndexView.update();
		mADSwitcher.showNext();
		if(isFocused()){
			logd("send msg");
			mHandler.removeMessages(MSG_AD_UPDATE);
			Message m = mHandler.obtainMessage(MSG_AD_UPDATE);
			mHandler.sendMessageDelayed(m, 5000);
		}
	}
	
	private void openADLink(){
		mApp.openADLink(indexToKey(mADIndex));
	}
	
	/*public void ADDataUpdate(){
		int count = mApp.getADnumber();
		mADIndex = 0;
		mADIndexView.setCount(Math.max(count, 1));
		mADSwitcher.setDisplayedChild(0);
		
		updateADView();
	}*/
	
	private final int MSG_AD_UPDATE = 200;
	
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			switch(msg.what){
			case MSG_AD_UPDATE:{
				mApp.ADDebug("MSG_AD_UPDATE:mApp="+mApp);
				if(isFocused()){
					int count = mApp.getADnumber();
					
					if(mADCount != count){
						mADIndex = 0;
						mADIndexView.removeAllViews();
						mADCount = count;
						mADIndexView.setCount(Math.max(mADCount, 1));
						if(mADCount > 1){
							mADIndexView.setVisibility(View.VISIBLE);
						}else{
							mADIndexView.setVisibility(View.INVISIBLE);
						}
						mADIndexView.setCurrent(mADIndex);
						mADIndexView.update();
						mADSwitcher.setDisplayedChild(0);
						updateADView();
						break;
					}
					
					if(count <= 1){
						//only one
						if(isFocused()){
							logd("send msg");
							mHandler.removeMessages(MSG_AD_UPDATE);
							Message m = mHandler.obtainMessage(MSG_AD_UPDATE);
							mHandler.sendMessageDelayed(m, 5000);
						}
						
						break;
					}
					mADIndex ++;
					mADIndex = mADIndex%count;
					mADSwitcher.setInAnimation(right2left_in);
					mADSwitcher.setOutAnimation(right2left_out);
					updateADView();
				}
			}
			break;
			}
		}
	};
	
	@Override
	public void setFocused(boolean set){
		logd("setFocused "+set);
		if(set){
			mHandler.removeMessages(MSG_AD_UPDATE);
			Message m = mHandler.obtainMessage(MSG_AD_UPDATE);
			mHandler.sendMessageDelayed(m, 5000);
		}else{
			mHandler.removeMessages(MSG_AD_UPDATE);
		}
		super.setFocused(set);
	}
	
	@Override
	public void onResume(){
		logd("onResume mApp=" + mApp);
		super.onResume();
		//resizeCardView();
		updateADView();
		//clearADView();
	}

	@Override
	public void onPause(){
		logd("onPause");
		clearADView();
		super.onPause();
		//setFocused(false);
	}
	
	@Override
	public void onDestroyView(){
		logd("onDestroyView");
		mHandler.removeMessages(MSG_AD_UPDATE);
		super.onDestroyView();
	}
	
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.toindex:{
			((BaseHomeActions)getActivity()).quitToIndexActivity();
		}break;
		case R.id.scan:{
			((BaseHomeActions)getActivity()).openPopMenu("home",scanView);
		}
		break;
		case R.id.to_cards:
		case R.id.card_act_view:{
			if(mApp.isUserSignIn()){
				((BaseHomeActions)getActivity()).openCardsActivity(null);
			}else{
				((BaseHomeActions)getActivity()).enterSignIn();
			}
		}break;
		case R.id.to_map:
		case R.id.map_act_view:{
			if(mApp.isUserSignIn()){
				((HomeActivity)getActivity()).openMapActivity();
			}else{
				((BaseHomeActions)getActivity()).enterSignIn();
			}
		}break;
		}
	}

	/*@Override
	public boolean onTouch(View v, MotionEvent e) {
		if(e.getAction() == MotionEvent.ACTION_DOWN){
			if(v.getId() == R.id.to_map){
				to_map.setBackgroundColor(getResources().getColor(R.color.DIALOG_FG_COLOR));
				v.onTouchEvent(e);
				return true;
			}else if(v.getId() == R.id.to_cards){
				to_cards.setBackgroundColor(getResources().getColor(R.color.DIALOG_FG_COLOR));
				v.onTouchEvent(e);
				return true;
			}
		}else if((e.getAction() == MotionEvent.ACTION_UP) || (e.getAction() == MotionEvent.ACTION_CANCEL)){
			if(v.getId() == R.id.to_map){
				to_map.setBackgroundColor(0xffffffff);
				v.onTouchEvent(e);
				return true;
			}else if(v.getId() == R.id.to_cards){
				to_cards.setBackgroundColor(getResources().getColor(R.color.LIGHT_GRAY));
				v.onTouchEvent(e);
				return true;
			}
		}
		
		return false;
	}*/
}
